package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.demo.hydrology.dto.CgkcdycDto;
import org.jeecg.modules.demo.hydrology.dto.CgkyjDto;
import org.jeecg.modules.demo.hydrology.entity.Cgkcdyc;
import org.jeecg.modules.demo.hydrology.entity.Cgkyj;
import org.jeecg.modules.demo.hydrology.mapper.CgkyjMapper;
import org.jeecg.modules.demo.hydrology.service.ICgkyjService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 长观孔预警
 * @Author: jeecg-boot
 * @Date:   2024-05-23
 * @Version: V1.0
 */
@Service
public class CgkyjServiceImpl extends ServiceImpl<CgkyjMapper, Cgkyj> implements ICgkyjService {
    @Autowired
    private CgkyjMapper cgkyjMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    /**
     * 长观孔预警-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<CgkyjDto> getList(Page<Cgkyj> page, QueryWrapper<Cgkyj> queryWrapper) {
        Page<Cgkyj> cgkyjPage = cgkyjMapper.selectPage(page, queryWrapper);
        List<CgkyjDto> collect = cgkyjPage.getRecords().stream().map(cgkyj -> {
            CgkyjDto cgkyjDto = new CgkyjDto();
            BeanUtils.copyProperties(cgkyj, cgkyjDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", cgkyj.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                cgkyjDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","alarm_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",cgkyj.getAlarmStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    cgkyjDto.setAlarmStatusItemText(sysDictItem.getItemText());
                }
            }
            return cgkyjDto;
        }).collect(Collectors.toList());
        IPage<CgkyjDto> cgkyjDtoIPage = new Page<>();
        cgkyjDtoIPage.setRecords(collect);
        cgkyjDtoIPage.setTotal(cgkyjPage.getTotal());
        cgkyjDtoIPage.setPages(page.getPages());
        cgkyjDtoIPage.setSize(page.getSize());
        return cgkyjDtoIPage;
    }

    /**
     * 修改告警状态
     * @param id
     * @param alarmStatus
     */
    @Override
    public void updateStatus(String id, String alarmStatus) {
        cgkyjMapper.update(new Cgkyj().setAlarmStatus(alarmStatus),
                new UpdateWrapper<Cgkyj>().lambda().eq(Cgkyj::getId,id));
    }
}
